package com.moyou.weichat;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.moyou.weichat.chat.ChatManger;
import com.moyou.weichat.listener.HxRegisterHandler;
import com.moyou.weichat.listener.RegisterTextWatcherListener;
import com.moyou.weichat.listener.TextWatcherListener;
import com.moyou.weichat.utils.ConstantUtils;
import com.moyou.weichat.utils.T;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * sj ：2017年10月16日11:03:11
 * name: ts
 * 用途:注册页面
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.title_name)
    TextView titleName;
    @Bind(R.id.title_left_view)
    ImageView titleLeftView;
    //账号和密码的输入框
    @Bind(R.id.register_phone)
    EditText edRegisterPhone;
    @Bind(R.id.register_pwd)
    EditText edRegisterPwd;
    //注册的按钮
    @Bind(R.id.register_btn)
    Button buRegisterBtn;

    private HxRegisterHandler hxRegisterHandler; //注册成功的通知类



    @Override
    protected int getLayout() {
        return R.layout.activity_registered;
    }

    @Override
    protected void initTitle() {
        titleName.setText("注册");
        titleLeftView.setImageResource(R.mipmap.ic_return);
    }

    @Override
    protected void initListener() {
        edRegisterPhone.addTextChangedListener(new RegisterTextWatcherListener(edRegisterPwd,buRegisterBtn, ConstantUtils.LOGIN_TYPE_NAME,mContext));
        edRegisterPwd.addTextChangedListener(new RegisterTextWatcherListener(edRegisterPhone,buRegisterBtn, ConstantUtils.LOGIN_TYPE_PASS,mContext));
    }

    @Override
    protected void initEventAndData() {
        hxRegisterHandler = new HxRegisterHandler(this);
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.LEFT;
    }

    @Override
    protected void createPresenter() {

    }
    @OnClick({R.id.register_btn,R.id.title_left_view})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.register_btn:
                //注册的逻辑
                final String userName  = edRegisterPhone.getText().toString();
                final String userPass  = edRegisterPwd.getText().toString();
                //判断位数
                if (userName.length() <= 0 && userPass.length()<=  0) {
                    showToast("请输入用户名和密码!");
                    return;
                }
                dialogShow();
                //环信注册需要在子线程中处理
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ChatManger.getChatManger().registerHx(userName,userPass);
                            hxRegisterHandler.sendEmptyMessage(ConstantUtils.HANDLER_SUCCESS);//注册成功
                        } catch (HyphenateException e) {
                            hxRegisterHandler.sendEmptyMessage(ConstantUtils.HANDLER_Fali);//注册失败
                        }
                    }
                }).start();
                break;
            case R.id.title_left_view:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hxRegisterHandler = null;
    }
}
